package com.spring.service.impl;

import com.spring.dao.AccountDao;

import com.spring.entity.Account;
import com.spring.service.AccountService;


/**
 * 问题：每个方法都写txManagement 事务方法
 *          代码比较臃肿，不易维护
 */

public class AccountServiceImpl implements AccountService {

    private AccountDao accountDao;



    public Account findAccountByName(String accountName) {
            Account accountByName = accountDao.findAccountByName(accountName);
            return accountByName;
    }

    public void updateAccount(Account account) {

            accountDao.updateAccount(account);
        }




    public void transfer(String sourceName, String targetName, Float money) {

            //根据A名称查询出账户
            Account source = accountDao.findAccountByName(sourceName);
            //根据B名称查询出账户
            Account target = accountDao.findAccountByName(targetName);
            //转出账户减钱
            source.setMoney(source.getMoney() - money);
            //转入账户加钱
            target.setMoney(target.getMoney() + money);
            int k = 1/0;
            //更新转出账户
            accountDao.updateAccount(source);

            //更新转入账户
            accountDao.updateAccount(target);

    }


    public AccountDao getAccountDao() {
        return accountDao;
    }

    public void setAccountDao(AccountDao accountDao) {
        this.accountDao = accountDao;
    }
}
